import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# 设置样式和配色
sns.set_style('whitegrid')
sns.set_palette('husl')
plt.rcParams['font.sans-serif'] = ['Source Han Serif SC'] # Seaborn 重置样式后重新绑定已安装中文字体
plt.rcParams['axes.unicode_minus'] = False # 保持坐标轴负号正确显示
fig, ax = plt.subplots(figsize=(10, 3.8))
# 使用离线中性示例数据,避免渲染依赖 Seaborn 在线数据仓库
data_plot = pd.DataFrame({
'total_bill': [10.5, 13.2, 18.7, 21.4, 24.8, 28.1, 31.6, 35.2],
'tip': [1.6, 2.0, 2.7, 3.1, 3.8, 4.2, 4.8, 5.3],
'day': ['Thur', 'Thur', 'Fri', 'Fri', 'Sat', 'Sat', 'Sun', 'Sun'],
'size': [2, 2, 2, 3, 3, 4, 4, 5],
})
# 绘制多维散点图:颜色=星期,大小=人数
sns.scatterplot(data=data_plot, x='total_bill', y='tip', hue='day', size='size', ax=ax)
ax.set_title('餐厅小费映射|教学示例,非业务观测', fontsize=16)
ax.set_xlabel('总账单', fontsize=16)
ax.set_ylabel('小费', fontsize=16)
ax.legend(title='星期', loc='center left', bbox_to_anchor=(1.01, 0.5), frameon=True) # 将图例移出坐标区,避免遮挡高值点与刻度
fig.tight_layout(rect=(0, 0, 0.82, 1)) # 为外置图例预留右侧空间
plt.show()